package admin_user_api

import (
	"blog_go/models"
	"blog_go/models/ctype"
	"blog_go/models/res"
	"blog_go/service/common"
	"blog_go/utils"
	"github.com/gin-gonic/gin"
	"github.com/jinzhu/copier"
	"github.com/sirupsen/logrus"
	"time"
)

type UserListRsp struct {
	ID         uint             `json:"id"`         // 主键id
	CreatedAt  time.Time        `json:"created_at"` // 创建时间 默认datetime(3)
	NickName   string           `json:"nick_name"`
	UserName   string           `json:"user_name"`
	Password   string           `json:"password"`
	Avatar     string           `json:"avatar"`
	Email      string           `json:"email"`
	Tel        string           `json:"tel"`
	Addr       string           `json:"addr"`
	Token      string           `json:"token"`
	IP         string           `json:"ip"`
	Role       ctype.Role       `json:"role"`
	SignStatus ctype.SignStatus `json:"sign_status"`
}

func (AdminUserApi) UserList(c *gin.Context) {
	page := models.DefaultPageInfo()
	if err := c.ShouldBindQuery(&page); err != nil {
		logrus.Error(err)
		panic(err)
	}
	list, count, err := common.ComList(models.UserModel{}, page)
	if err != nil {
		logrus.Error(err)
		panic(err)
	}
	// 这里和java不一样,因为是值传递,所以修改user值对本身不产生影响
	var rspList []UserListRsp
	for _, user := range list {
		user.Tel = utils.TelConvert(user.Tel)
		user.Email = utils.EmailCovert(user.Email)
		var rsp UserListRsp
		copier.Copy(&rsp, &user)
		rspList = append(rspList, rsp)
	}
	res.OkWithList(rspList, count, c)
}
